package com.yc.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yc.domain.Oprecord;
import com.yc.dao.OPrecordDao;
import com.yc.model.JsonModel;
import com.yc.service.EmailBiz;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-08-22 20:48
 */
@RestController
@RequestMapping("/email")
public class SendController {
    @Autowired
    private EmailBiz mailBiz;
    @Autowired
    private OPrecordDao oprecordDao;

    @RequestMapping("/websocket")
    public JsonModel send() {
        JsonModel jsonModel = new JsonModel();
        long count = mailBiz.getMailCount();
        jsonModel.setCode(1);
        jsonModel.setObj(count);
        return jsonModel;
    }

    @RequestMapping("/info")
    public JsonModel getServerInfo(HttpServletRequest req) {
        //获取服务器信息
        Map<String, Object> map = new HashMap<>();
        String protocol = req.isSecure() ? "wss" : "ws";
        String host = req.getServerName();
        int port = req.getServerPort();
        String contextPath = req.getContextPath();
        map.put("protocol", protocol);
        map.put("host", host);
        map.put("port", port);
        map.put("contextPath", contextPath);
        JsonModel jm = new JsonModel();
        jm.setCode(1);
        jm.setObj(map);
        return jm;
    }
    @RequestMapping("/message")
    public JsonModel getMessage() {
        JsonModel jm = new JsonModel();
        //获取oprecord表中accountid、opmoney、optime字段的数据
        QueryWrapper<Oprecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","accountid", "opmoney", "optime","optype").orderByDesc("id");
        List<Oprecord> list = oprecordDao.selectList(queryWrapper);
        //获取list最后十条数据
        list = list.subList(0, 10);
        jm.setCode(1);
        jm.setObj(list);
        return jm;
    }
}
